﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 图书管理系统
{
    public partial class 书籍详情 : Form
    {
        books books;
        int method;
        String content;
        public 书籍详情(books books,int method,String content)
        {
            InitializeComponent();
            this.books =books;
            this.method = method;
            this.content = content;
        }
        public 书籍详情()
        {
            InitializeComponent();
           
        }
        private void label14_Click(object sender, EventArgs e)
        {

        }

        private void 书籍详情_Load(object sender, EventArgs e)
        {
            List<Book> Book = books.GetBooks1;
            label20.Text = Book.Count.ToString();
            label18.Text = ((int)Math.Ceiling(Book.Count * 1.0 / 4)).ToString();
            label9.Text = "1";

            label1.Text = books.BookName1;
            label4.Text = books.Author1;
            label5.Text = books.Press1;
            label11.Text = books.ISBN1;
            label13.Text = books.BookIntro1;
            label8.Text = Book.Count.ToString();

            GroupBox gbox = new GroupBox();
            gbox.Name = 1.ToString();
            gbox.Text = "";
            gbox.Width = 680; 
            gbox.Height = 179;
            gbox.Location = new Point(39, 425);
            this.Controls.Add(gbox);
            int k = Book.Count;
            if (Book.Count > 4)
            {
                k = 4;
            }
            for (int i=0;i<k;i++)
            {
                Book book = Book[i];
                String BookLocation = "地点:";
                Label Location = new Label();
                Location.Name = BookLocation + book.BookLocation1 + i;
                Location.Text = book.BookLocation1;
                Location.Location = new Point(35, 30+30*i);

               
                String BookNum = "索书号:";
                Label Booknumber = new Label();
                Booknumber.Name = BookNum + book.BookNum1 + i;
                Booknumber.Text = book.BookNum1;
                Booknumber.Location = new Point(198, 30 + 30 * i);

                String BookS = "状态";
                Label BookStatus = new Label();
                BookStatus.Name =BookS + i.ToString();
                BookStatus.Text = book.BookStatus1;
                BookStatus.Location = new Point(398, 30 + 30 * i);

                Button button = new Button();
                button.Name = i.ToString();
                button.Text = "借书";
                button.Location = new Point(530, 30 + 30 * i);
                button.Width = 74;
                button.Height = 33;
                button.Click += new EventHandler(jieshu_Click);

                Button yuyue = new Button();
                yuyue.Name = i.ToString();
                yuyue.Text = "预约";
                yuyue.Location = new Point(600, 30 + 30 * i);
                yuyue.Width = 74;
                yuyue.Height = 33;

                yuyue.Click += new EventHandler(yuyue_Click);

                gbox.Controls.Add(Location);
                gbox.Controls.Add(Booknumber);
                gbox.Controls.Add(BookStatus);
                gbox.Controls.Add(button);
                gbox.Controls.Add(yuyue);
            }
           
        }
        private void jieshu_Click(object sender, EventArgs e)
        {
            List<Book> Book = books.GetBooks1;
            int BookNum = int.Parse(((Button)sender).Name);
            User user1 = 用户登录.user;
            Book b = Book[BookNum];
            if (Book[BookNum].BookStatus1.Trim()=="不可借")
            {
                BorrowDao borrowDao = new BorrowDao();
                if (borrowDao.juage(user1.Id,b.BookNum1) == 1)
                {
                    Book book = Book[BookNum];
                    User user = 用户登录.user;
                    if (user.Id == null)
                    {
                        MessageBox.Show("请登录");
                    }
                    else
                    {
                        UserDao userDao = new UserDao();
                        if (userDao.judge(user.Id, user.Identy) == true)
                        {
                            PenaltyrecordDao dao = new PenaltyrecordDao();
                            if (dao.judgeUser(user.Id) == "0")
                            {
                                if (borrowDao.input_Borrowrecord(user.Id, book.BookNum1, user.Identy) == 1)
                                {
                                    MessageBox.Show("借书成功");
                                    ((Button)sender).Text = "不可借";
                                    foreach (Control i in this.Controls)
                                    {
                                        if (i is GroupBox)
                                        {
                                            foreach (Control k in i.Controls)
                                            {
                                                if (k is Label)
                                                {
                                                    if (k.Name == ("状态" + ((Button)sender).Name))
                                                    {
                                                        k.Text = "不可借";
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    BookDao bookDao = new BookDao();
                                    if (bookDao.updateStatus(book.BookNum1) > 0)
                                    {
                                       
                                        book.BookStatus1 = "不可借";
                                        books.Kejiesum = books.Kejiesum - 1;
                                    }

                                    userDao.updateBorrow_num(user.Id);

                                }
                                else
                                {
                                    MessageBox.Show("借书失败！");
                                }
                            }
                            else
                            {
                                MessageBox.Show("存在欠费记录,请先缴费");
                            }
                        }
                        else
                        {
                            MessageBox.Show("读者可借次数已满！");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("该书不可借");
                }

            }
            else
            {
                Book book = Book[BookNum]; 
                BorrowDao borrowDao = new BorrowDao(); 
                User user = 用户登录.user;
                if(user.Id==null)
                {
                    MessageBox.Show("请登录");
                }
                else
                {
                    UserDao userDao = new UserDao();
                    if(userDao.judge(user.Id,user.Identy)==true)
                    {
                        PenaltyrecordDao dao = new PenaltyrecordDao();
                        if (dao.judgeUser(user.Id) == "0")
                        {
                            if (borrowDao.input_Borrowrecord(user.Id, book.BookNum1, user.Identy) == 1)
                            {
                                MessageBox.Show("借书成功");
                                ((Button)sender).Text = "不可借";
                                foreach (Control i in this.Controls)
                                {
                                    if (i is GroupBox)
                                    {
                                        foreach (Control k in i.Controls)
                                        {
                                            if (k is Label)
                                            {
                                                if (k.Name == ("状态" + ((Button)sender).Name))
                                                {
                                                    k.Text = "不可借";
                                                }
                                            }
                                        }
                                    }
                                }
                                BookDao bookDao = new BookDao();
                                if (bookDao.updateStatus(book.BookNum1) > 0)
                                {
                                    MessageBox.Show("修改图书状态成功！");
                                    book.BookStatus1 = "不可借";
                                    books.Kejiesum = books.Kejiesum - 1;
                                }

                                userDao.updateBorrow_num(user.Id);

                            }
                            else
                            {
                                MessageBox.Show("借书失败！");
                            }
                        }
                        else
                        {
                            MessageBox.Show("存在欠费记录,请先缴费");
                        }
                    }
                    else
                    {
                        MessageBox.Show("读者可借次数已满！");
                    }
                }
               
            }
        }

        private void label19_Click(object sender, EventArgs e)
        {

        }
        private void yuyue_Click(object sender, EventArgs e)
        {
            List<Book> Book = books.GetBooks1;
            int Index = int.Parse(((Button)sender).Name);
            Book book = Book[Index];
            String BookNum = book.BookNum1;
            User user = 用户登录.user;
            SqlConnection conn = new SqlConnection();
            String id = user.Id;
            if (id == null)
            {

      
                MessageBox.Show("当前未登录");
                
            }
            else
            {

                String isbn = label11.Text;
                String bookname = label1.Text;

                string sql = "select * from Readerinfo where ReaderID='" + id.ToString() + "'";
                SqlCommand cmd = Dao.SqlConnect(sql, conn);


                string sql1 = "select count(*) val_count from Appointmentrecord_Table where ReaderID='" + id.ToString() + "' and OrderState='预约中'";
                SqlCommand cmd1 = Dao.SqlConnect(sql1, conn);

                string sql2 = "select count(*) val_count from Appointmentrecord_Table where BookNum='" + BookNum + "' and OrderState='预约中'";
                SqlCommand cmd2 = Dao.SqlConnect(sql2, conn);

                SqlConnection conn16 = new SqlConnection();
                SqlConnection conn4 = new SqlConnection();
                try
                {
                    //获取用户信息
                    DataSet dt = new DataSet();
                    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd);
                    conn.Open();
                    sqlDataAdapter.Fill(dt, "Readerinfo");
                    String name = dt.Tables["Readerinfo"].Rows[0]["ReaderName"].ToString();
                    String tel = dt.Tables["Readerinfo"].Rows[0]["ReaderTel"].ToString();

                    string sql16 = "select * from Borrowingrules_Table where Identy='" + user.Identy + "'";
                    SqlCommand cmd16 = Dao.SqlConnect(sql16, conn16);
                    conn16.Open();

                    SqlDataReader reader16 = cmd16.ExecuteReader();
                    String bnum = null;
                    String onum = null;
                    while (reader16.Read())
                    {
                        bnum = reader16[0].ToString();
                        onum = reader16[1].ToString();
                    }
                    int a = Convert.ToInt32(bnum);
                    int b = Convert.ToInt32(onum);
                    //查询用户当前可预约数
                    int val_count = (int)cmd1.ExecuteScalar();
                    int real_val_count = b - val_count;
                    String s1 = Convert.ToString(real_val_count);

                    //查询还有多少人排队
                    String inline = cmd2.ExecuteScalar().ToString();
                    //查询是否已预约过这本书
                    string sql4 = "select * from Appointmentrecord_Table where BookNum='" + BookNum + "'and OrderState ='预约中' and ReaderID='" + user.Id + "'";
                    SqlCommand cmd4 = Dao.SqlConnect(sql4, conn4);
                    conn4.Open();
                    SqlDataReader sdr = cmd4.ExecuteReader();

                    if (sdr.HasRows)
                    {
                        MessageBox.Show("您已经预约过这本书了");
                        conn4.Close();
                    }
                    else
                    {
                        预约 form = new 预约(name, tel, s1, isbn, bookname, inline, BookNum,books, Index, method,content);
                        form.Show();
                        this.Close();
                        conn4.Close();
                    }
                    

                    
                    //this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (label9.Text == "1")
            {
                MessageBox.Show("已经是第一页了！");
            }
            else
            {

                for (int j = 0; j < 10; j++)
                {
                    foreach (Control i in this.Controls)
                    {
                        if (i is GroupBox)
                        {           
                                this.Controls.Remove(i);
                                i.Dispose();
                        }
                    }
                }
                int currentPage = int.Parse(label9.Text);
                currentPage -= 1;
                label9.Text = currentPage.ToString();
                List<Book> Book = books.GetBooks1;
                GroupBox gbox = new GroupBox();
                gbox.Name = 1.ToString();
                gbox.Text = "";
                gbox.Width = 680;
                gbox.Height = 179;
                gbox.Location = new Point(39, 425);
                this.Controls.Add(gbox);
                for (int i = 4 * (currentPage - 1); i < currentPage * 4; i++)
                {
                    Book book = Book[i];
                    String BookLocation = "地点:";
                    Label Location = new Label();
                    Location.Name = BookLocation + book.BookLocation1 + i;
                    Location.Text = book.BookLocation1;
                    Location.Location = new Point(35, 30 + 30 * (i - 4 * (currentPage - 1)));


                    String BookNum = "索书号:";
                    Label Booknumber = new Label();
                    Booknumber.Name = BookNum + book.BookNum1 + i;
                    Booknumber.Text = book.BookNum1;
                    Booknumber.Location = new Point(198, 30 + 30 * (i - 4 * (currentPage - 1)));

                    String BookS = "状态";
                    Label BookStatus = new Label();
                    BookStatus.Name = BookS + i.ToString();
                    BookStatus.Text = book.BookStatus1;
                    BookStatus.Location = new Point(398, 30 + 30 * (i - 4 * (currentPage - 1)));

                    Button button = new Button();
                    button.Name = i.ToString();
                    button.Text = "借书";
                    button.Location = new Point(530, 30 + 30 * (i - 4 * (currentPage - 1)));
                    button.Width = 74;
                    button.Height = 33;
                  
                    button.Click += new EventHandler(jieshu_Click);

                    Button yuyue = new Button();
                    yuyue.Name = i.ToString();
                    yuyue.Text = "预约";
                    yuyue.Location = new Point(600, 30 + 30 * (i - 4 * (currentPage - 1)));
                    yuyue.Width = 74;
                    yuyue.Height = 33;

                    yuyue.Click += new EventHandler(yuyue_Click);

                    gbox.Controls.Add(Location);
                    gbox.Controls.Add(Booknumber);
                    gbox.Controls.Add(BookStatus);
                    gbox.Controls.Add(button);
                    gbox.Controls.Add(yuyue);
                }
            }
        }
       
        private void button2_Click(object sender, EventArgs e)
        {
            if (label18.Text == label9.Text)
            {
                MessageBox.Show("已经是最后一页了！");
            }
            else
            {
                for (int j = 0; j < 10; j++)
                {
                    foreach (Control i in this.Controls)
                    {
                        if (i is GroupBox)
                        {

                            this.Controls.Remove(i);
                            i.Dispose();
                        }
                    }

                }
                int currentPage = int.Parse(label9.Text);
                currentPage += 1;
                label9.Text = currentPage.ToString();
                List<Book> Book = books.GetBooks1;
                GroupBox gbox = new GroupBox();
                gbox.Name = 1.ToString();
                gbox.Text = "";
                gbox.Width = 680;
                gbox.Height = 179;
                gbox.Location = new Point(39, 425);
                this.Controls.Add(gbox);
                int k = Book.Count;
                if (k > 4 * currentPage)
                {
                    k = 4 * currentPage;
                }
                for (int i = 4 * (currentPage - 1); i < k; i++)
                {
                    Book book = Book[i];
                    String BookLocation = "地点:";
                    Label Location = new Label();
                    Location.Name = BookLocation + book.BookLocation1 + i;
                    Location.Text = book.BookLocation1;
                    Location.Location = new Point(35, 30 + 30 * (i - 4 * (currentPage - 1)));


                    String BookNum = "索书号:";
                    Label Booknumber = new Label();
                    Booknumber.Name = BookNum + book.BookNum1 + i;
                    Booknumber.Text = book.BookNum1;
                    Booknumber.Location = new Point(198, 30 + 30 * (i - 4 * (currentPage - 1)));

                    String BookS = "状态";
                    Label BookStatus = new Label();
                    BookStatus.Name = BookS + i.ToString();
                    BookStatus.Text = book.BookStatus1;
                    BookStatus.Location = new Point(398, 30 + 30 * (i - 4 * (currentPage - 1)));

                    Button button = new Button();
                    button.Name = i.ToString();
                    button.Text = "借书";
                    button.Location = new Point(530, 30 + 30 * (i - 4 * (currentPage - 1)));
                    button.Width = 74;
                    button.Height = 33;
                   
                    button.Click += new EventHandler(jieshu_Click);

                    Button yuyue = new Button();
                    yuyue.Name = i.ToString();
                    yuyue.Text = "预约";
                    yuyue.Location = new Point(600, 30 + 30 * (i - 4 * (currentPage - 1)));
                    yuyue.Width = 74;
                    yuyue.Height = 33;

                    yuyue.Click += new EventHandler(yuyue_Click);

                    gbox.Controls.Add(Location);
                    gbox.Controls.Add(Booknumber);
                    gbox.Controls.Add(BookStatus);
                    gbox.Controls.Add(button);
                    gbox.Controls.Add(yuyue);
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            String key=null;
            if(method==1)
            {
                key = "关键字";
            }
            if(method==2)
            {
                key = "ISBN";
            }
            if(method==3)
            {
                key = "作者名";
            }
            查询书籍 form1 = new 查询书籍(key,content);
            form1.Show();
            this.Close();
        }
    }
}
